
Partial Class RIAssessments_PrintableCopy
    Inherits System.Web.UI.Page

    Public assessmentActivityValue As Cdm.RIDataCore.Assessments.AssessmentActivityValue
    Protected assessment As Cdm.RIDataCore.Assessments.Assessment

    Protected ReadOnly Property AssessmentEditable() As Boolean
        Get
            Return (((UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.SysAdmin) Or _
                     (UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.SupportBroker) Or _
                     (UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.SupportBrokerAdmin) Or _
                     (UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.StateOffice) Or _
                     (UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.StateAdmin)) And _
                     (IIf(assessment IsNot Nothing, assessment.BudgetId = 0, False)))
        End Get
    End Property

    Protected Sub Page_Init(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Init

        Try

            If Session("UserMessage") IsNot Nothing Then
                Me.ShowMessage(Session("UserMessage"), Drawing.Color.Navy)
                Session("UserMessage") = Nothing
            End If

            '   This page must be loaded on every postback in order to keep the dynamically loaded usercontrol
            '   templates in the grid and be able to reference their values.

            If Request.QueryString("aid") IsNot Nothing Then
                Me.LoadAssessment(Page.IsPostBack)
            End If

            Me.CalculatedBudgetValueLabel.Text = Me.GetCalculatedBudget
            'If UserAuthorization.HasUpdatePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.ApproveAssessments) Then
            Me.ApprovedBudgetPanel.Visible = True
            Me.ApprovedBudgetValueLabel.Text = Me.GetApprovedBudget
            'Else
            'Me.ApprovedBudgetPanel.Visible = False
            'End If

            Me.PrintButton.Attributes.Add("onclick", "javascript: window.print(); return false;")

            Dim participant As Cdm.DataCore.Entities.Participant = Cdm.DataCore.Entities.Participant.Retrieve(assessment.ParticipantId)
            NameLabel.Text = "<b>Participant Name:</b> " & participant.FirstName & " " & participant.LastName
            MedicaidIdLabel.Text = "<b>Medicaid ID:</b> " & participant.MedicaidNumber.ToString

            Me.FuncCharCheckBoxList.Enabled = Me.AssessmentEditable

        Catch ex As Gentle.Common.GentleException

            ' This exception will occur if someone tries to manually put an invalid assessment Id in the query string.
            Me.MessageLabel.Visible = True
            Me.MessageLabel.Text = "Assessment failed to load."

        End Try

    End Sub

    Protected Sub LoadAssessment(ByVal isPostback As Boolean)

        ' Load the selected assessment.
        assessment = Cdm.RIDataCore.Assessments.Assessment.Retrieve(CInt(Request.QueryString("aid")))

        Me.HeaderLabel.Text = "Assessment - " & CDate(assessment.AssessmentDate).ToShortDateString
        Me.FuncCharCheckBoxList.DataSource = Cdm.RIDataCore.Assessments.FunctionalCharacteristicDisplay.ListByAssessmentId(CInt(Request.QueryString("aid")), "Description")
        Me.FuncCharCheckBoxList.DataTextField = "Description"
        Me.FuncCharCheckBoxList.DataValueField = "FuncCharId"
        Me.FuncCharCheckBoxList.DataBind()
        If Not isPostback Then
            Me.SelectCheckboxes()
        End If
        Me.BindADLSGrid()
        Me.BindIADLSGrid()

    End Sub

    Protected Sub BindADLSGrid()

        Me.AdlsAssessmentActivityGridView.DataSource = Cdm.RIDataCore.Assessments.AssessmentActivityDisplay.ListByAssessmentCategoryId(Cdm.RIDataCore.Assessments.AssessmentCategory.Retrieve("ADLS").AssessmentCategoryId, Request.QueryString("aid"), "Description")
        Me.AdlsAssessmentActivityGridView.DataBind()

    End Sub

    Protected Sub BindIADLSGrid()

        Me.IadlsAssessmentActivityGridView.DataSource = Cdm.RIDataCore.Assessments.AssessmentActivityDisplay.ListByAssessmentCategoryId(Cdm.RIDataCore.Assessments.AssessmentCategory.Retrieve("IADLS").AssessmentCategoryId, Request.QueryString("aid"), "Description")
        Me.IadlsAssessmentActivityGridView.DataBind()

    End Sub

    Protected Sub AssessmentActivityGridView_RowDataBound(ByVal sender As Object, _
                                                          ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
                                                          Handles AdlsAssessmentActivityGridView.RowDataBound, _
                                                                  IadlsAssessmentActivityGridView.RowDataBound

        ' Hide columns that are only used for reference.
        e.Row.Cells(2).Style("display") = "none"
        e.Row.Cells(3).Style("display") = "none"

        If e.Row.RowType = DataControlRowType.DataRow Then

            ' Determine if we're retrieving the current assessment or previous assessment.
            If CInt(Request.QueryString("aid")) <> 0 Then
                assessmentActivityValue = Cdm.RIDataCore.Assessments.AssessmentActivityValue.Retrieve(CInt(e.Row.Cells(2).Text), CInt(Request.QueryString("aid")))
            ElseIf CInt(Request.QueryString("paid")) <> 0 Then
                assessmentActivityValue = Cdm.RIDataCore.Assessments.AssessmentActivityValue.Retrieve(CInt(e.Row.Cells(2).Text), CInt(Request.QueryString("paid")))
            End If

            ' Get a reference to the loaded control and the plus image.
            Dim loadedControlPanel As Panel = CType(e.Row.Cells(1).Controls(1), Panel)
            Dim ctrlToLoad As Control = Me.Page.LoadControl(e.Row.Cells(3).Text)
            loadedControlPanel.Controls.Add(ctrlToLoad)
            loadedControlPanel.Style("display") = "inline"

            Dim assessmentEditable As Boolean = Me.AssessmentEditable

            Select Case e.Row.Cells(3).Text

                Case "TemplateOne.ascx"

                    ' Cast the loaded control to the class TemplateOne and load the appropriate values.
                    Dim ctrl As RIAssessments_TemplateOne = CType(ctrlToLoad, RIAssessments_TemplateOne)
                    If assessmentActivityValue IsNot Nothing Then
                        With assessmentActivityValue
                            ctrl.LevelOfAssistanceId = .LevelOfAssistanceId
                            ctrl.TimesPerDay = IIf(.TimesPerDay = -1, "", .TimesPerDay.ToString)
                            ctrl.DaysPerWeek = IIf(.DaysPerWeek = -1, "", .DaysPerWeek.ToString)
                            ctrl.Comments = .Comments
                            ctrl.IsPrintPage = True
                        End With
                    End If
                    If Not assessmentEditable Then
                        ctrl.DisableFields()
                    End If

                Case "TemplateTwo.ascx"

                    ' Cast the loaded control to the class TemplateTwo and load the appropriate values.
                    Dim ctrl As RIAssessments_TemplateTwo = CType(ctrlToLoad, RIAssessments_TemplateTwo)
                    If assessmentActivityValue IsNot Nothing Then
                        With assessmentActivityValue
                            ctrl.LevelOfAssitanceId = .LevelOfAssistanceId
                            ctrl.NumberOfRooms = IIf(.NumberOfRooms = -1, "", .NumberOfRooms.ToString)

                            ctrl.Comments = .Comments
                            ctrl.IsPrintPage = True
                        End With
                    End If
                    If Not assessmentEditable Then
                        ctrl.DisableFields()
                    End If

                Case "TemplateThree.ascx"

                    ' Cast the loaded control to the class TemplateThree and load the appropriate values.
                    Dim ctrl As RIAssessments_TemplateThree = CType(ctrlToLoad, RIAssessments_TemplateThree)
                    If assessmentActivityValue IsNot Nothing Then
                        With assessmentActivityValue
                            ctrl.LevelOfAssitanceId = .LevelOfAssistanceId
                            ctrl.NumberOfTasks = IIf(.NumberOfTasks = -1, "", .NumberOfTasks.ToString)

                            ctrl.Comments = .Comments
                            ctrl.IsPrintPage = True
                        End With
                    End If
                    If Not assessmentEditable Then
                        ctrl.DisableFields()
                    End If

                Case "TemplateFour.ascx"

                    ' Cast the loaded control to the class TemplateFour and load the appropriate values.
                    Dim ctrl As RIAssessments_TemplateFour = CType(ctrlToLoad, RIAssessments_TemplateFour)
                    If assessmentActivityValue IsNot Nothing Then
                        With assessmentActivityValue
                            ctrl.LevelOfAssitanceId = .LevelOfAssistanceId
                            ctrl.Comments = .Comments
                            ctrl.IsPrintPage = True
                        End With
                    End If
                    If Not assessmentEditable Then
                        ctrl.DisableFields()
                    End If

            End Select

        End If

    End Sub

    Protected Sub SelectCheckboxes()

        ' Check the appropriate check boxes in the check box list.  This does not seem to be a bindable property
        ' of a check box list.
        Dim funcChar As Cdm.RIDataCore.Assessments.FunctionalCharacteristic = Nothing
        For Each item As ListItem In Me.FuncCharCheckBoxList.Items
            funcChar = Cdm.RIDataCore.Assessments.FunctionalCharacteristic.Retrieve(item.Value)
            item.Selected = funcChar.HasNeed
        Next

    End Sub

    Private Sub ShowMessage(ByVal message As String, _
                            ByVal color As System.Drawing.Color)

        Me.MessageLabel.ForeColor = color
        Me.MessageLabel.Text = message
        Me.MessageLabel.Visible = True

    End Sub

    Protected Function GetCalculatedBudget() As String

        Dim total As Double = 0.0
        If assessment IsNot Nothing Then

            Dim activityBudget As Cdm.RIDataCore.Assessments.AssessmentActivityBudget
            For Each activityBudget In Cdm.RIDataCore.Assessments.AssessmentActivityBudget.ListByAssessmentId(assessment.AssessmentId)
                total += CDbl(activityBudget.BudgetCalculation)
            Next

        End If

        Return Format(total, "$###,###,##0.00")

    End Function

    Protected Function GetApprovedBudget() As String

        Dim total As Double = 0.0
        If assessment IsNot Nothing Then
            If assessment.BudgetId <> 0 Then
                Dim budget As Cdm.DataCore.Budget.Budget = Cdm.DataCore.Budget.Budget.Retrieve(assessment.BudgetId)
                total = budget.BudgetAmount
            End If
        End If
        Return Format(total, "$###,###,##0.00")

    End Function

End Class
